﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace HML.Design
{
    /// <summary>
    /// 透明度颜色编辑器窗体
    /// </summary>
    [ToolboxItem(false)]
    public class AlphaColorEditorForm : UserControl
    {
        #region 属性

        private Color alphaColor = Color.Empty;
        /// <summary>
        /// 颜色
        /// </summary>
        public Color AlphaColor
        {
            get { return this.alphaColor; }
        }

        #endregion

        #region 字段

        private IWindowsFormsEditorService edSvc;
        private TabControl tabControl;
        private TabPage custom_page;
        private AlphaColorEditorCustomColorControl lb_Custom;
        private TabPage web_page;
        private AlphaColorEditorWebColorControl lb_Web;
        private TabPage system_page;
        private AlphaColorEditorSystemColorControl lb_System;
        private GroupBox aGroupBox;
        private TrackBar aTrackBar;
        private Label aMaxLabel;
        private Label aMinLabel;
        private AlphaColorEditorPreviewControl preview;
        private Label aValueLabel;
        private Button saveButton;

        private string custom_page_str = "自定义";
        private string web_page_str = "Web";
        private string system_page_str = "系统";

        private int currentAlpha = byte.MaxValue;
        private Color currentValue = Color.Empty;

        #endregion

        public AlphaColorEditorForm()
        {
            this.SuspendLayout();

            this.tabControl = new TabControl();
            this.custom_page = new TabPage();
            this.lb_Custom = new AlphaColorEditorCustomColorControl();
            this.web_page = new TabPage();
            this.lb_Web = new AlphaColorEditorWebColorControl();
            this.system_page = new TabPage();
            this.lb_System = new AlphaColorEditorSystemColorControl();
            this.aGroupBox = new GroupBox();
            this.aValueLabel = new Label();
            this.aMaxLabel = new Label();
            this.aMinLabel = new Label();
            this.aTrackBar = new TrackBar();
            this.preview = new AlphaColorEditorPreviewControl();
            this.saveButton = new Button();

            // lb_Custom
            this.lb_Custom.Size = new Size(239, 216);
            this.lb_Custom.Location = new Point(0, 0);
            this.lb_Custom.Margin = new Padding(0);
            this.lb_Custom.Dock = DockStyle.Fill;
            // custom_page
            this.custom_page.Size = new Size(239, 216);
            this.custom_page.Location = new Point(4, 25);
            this.custom_page.Margin = new Padding(0);
            this.custom_page.Text = custom_page_str;
            this.custom_page.Tag = custom_page_str;
            this.custom_page.Controls.Add(this.lb_Custom);
            // lb_Web
            this.lb_Web.Size = new Size(239, 216);
            this.lb_Web.Location = new Point(0, 0);
            this.lb_Web.Margin = new Padding(0);
            this.lb_Web.BorderStyle = BorderStyle.FixedSingle;
            this.lb_Web.Dock = DockStyle.Fill;
            this.lb_Web.DrawMode = DrawMode.OwnerDrawFixed;
            this.lb_Web.FormattingEnabled = true;
            this.lb_Web.IntegralHeight = false;
            this.lb_Web.ItemHeight = 23;
            // web_page
            this.web_page.Size = new Size(239, 216);
            this.web_page.Location = new Point(4, 25);
            this.web_page.Margin = new Padding(0);
            this.web_page.Text = web_page_str;
            this.web_page.Tag = web_page_str;
            this.web_page.Controls.Add(this.lb_Web);
            // lb_System
            this.lb_System.Size = new Size(239, 216);
            this.lb_System.Location = new Point(0, 0);
            this.lb_System.Margin = new Padding(0);
            this.lb_System.BorderStyle = BorderStyle.FixedSingle;
            this.lb_System.Dock = DockStyle.Fill;
            this.lb_System.DrawMode = DrawMode.OwnerDrawFixed;
            this.lb_System.FormattingEnabled = true;
            this.lb_System.IntegralHeight = false;
            this.lb_System.ItemHeight = 23;
            // system_page
            this.system_page.Size = new Size(239, 216);
            this.system_page.Location = new Point(4, 25);
            this.system_page.Margin = new Padding(0);
            this.system_page.Text = system_page_str;
            this.system_page.Tag = system_page_str;
            this.system_page.Controls.Add(this.lb_System);
            // aMaxLabel
            this.aMaxLabel.AutoSize = true;
            this.aMaxLabel.Text = byte.MaxValue.ToString();
            this.aMaxLabel.Location = new Point(10, 18);
            // aMinLabel
            this.aMinLabel.AutoSize = true;
            this.aMinLabel.Text = byte.MinValue.ToString();
            this.aMinLabel.Location = new Point(19, 166);
            // aTrackBar
            this.aTrackBar.Orientation = Orientation.Vertical;
            this.aTrackBar.AutoSize = false;
            this.aTrackBar.Size = new Size(39, 145);
            this.aTrackBar.Location = new Point(14, 29);
            this.aTrackBar.Margin = new Padding(0);
            this.aTrackBar.LargeChange = 1;
            this.aTrackBar.Maximum = 255;
            this.aTrackBar.TickStyle = TickStyle.None;
            // aValueLabel
            this.aValueLabel.AutoSize = true;
            this.aValueLabel.Location = new Point(61, 166);
            // preview
            this.preview.Size = new Size(98, 140);
            this.preview.Font = new Font("宋体", 12F);
            this.preview.Location = new Point(56, 22);
            // aGroupBox
            this.aGroupBox.Text = "透明度";
            this.aGroupBox.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left)));
            this.aGroupBox.ForeColor = Color.Black;
            this.aGroupBox.Location = new Point(249, 12);
            this.aGroupBox.Size = new Size(171, 193);
            this.aGroupBox.Controls.Add(this.aMaxLabel);
            this.aGroupBox.Controls.Add(this.aMinLabel);
            this.aGroupBox.Controls.Add(this.aTrackBar);
            this.aGroupBox.Controls.Add(this.aValueLabel);
            this.aGroupBox.Controls.Add(this.preview);
            // tabControl
            this.tabControl.Size = new Size(247, 255);
            this.tabControl.Location = new Point(0, 0);
            this.tabControl.Margin = new Padding(0);
            this.tabControl.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left)));
            this.tabControl.Controls.Add(this.custom_page);
            this.tabControl.Controls.Add(this.web_page);
            this.tabControl.Controls.Add(this.system_page);
            // saveButton
            this.saveButton.Size = new Size(170, 35);
            this.saveButton.Location = new Point(250, 217);
            this.saveButton.Text = "确定";
            this.saveButton.UseVisualStyleBackColor = true;
            // AlphaColorEditorForm
            this.Size = new Size(430, 255);
            this.BackColor = SystemColors.Control;
            this.ForeColor = SystemColors.ControlText;
            this.Padding = new Padding(0);
            this.Controls.Add(this.tabControl);
            this.Controls.Add(this.aGroupBox);
            this.Controls.Add(this.saveButton);

            this.ResumeLayout();
        }

        #region 公开方法

        public void Start(IWindowsFormsEditorService edSvc, object value)
        {
            this.tabControl.SelectedIndexChanged -= this.tabControl_SelectedIndexChanged;
            this.lb_Custom.ItemClick -= this.lb_Custom_ItemClick;
            this.lb_Web.Click -= this.lb_Web_Click;
            this.lb_System.Click -= this.lb_System_Click;
            this.aTrackBar.Scroll -= this.aTrackBar_Scroll;
            this.saveButton.Click -= this.saveButton_Click;

            this.edSvc = edSvc;
            this.alphaColor = (Color)value;
            this.currentAlpha = ((Color)value).A;
            this.currentValue = this.ConvertUnAlphaColor(((Color)value));
            this.lb_Custom.SelectedItem = null;
            this.lb_Web.SelectedItem = null;
            this.lb_System.SelectedItem = null;
            this.aTrackBar.Value = this.currentAlpha;
            this.aValueLabel.Text = "当前值:" + this.aTrackBar.Value.ToString();
            this.aGroupBox.Visible = !this.IsEmptyOrTransparent((Color)value);
            this.preview.AlphaColor = (Color)value;

            TabPage tabPage = null;
            if (this.currentValue.IsSystemColor)
            {
                foreach (Color obj in this.lb_System.SystemColorValues)
                {
                    if (obj.Name == this.currentValue.Name)
                    {
                        this.lb_System.SelectedItem = this.currentValue;
                        tabPage = this.system_page;
                        break;
                    }
                }
            }
            if (tabPage == null)
            {
                foreach (Color obj in this.lb_Web.WebColorValues)
                {
                    if ((obj.A == this.currentAlpha && obj.R == this.currentValue.R && obj.G == this.currentValue.G && obj.B == this.currentValue.B))
                    {
                        this.lb_Web.SelectedItem = this.currentValue;
                        tabPage = this.web_page;
                        break;
                    }
                }
            }
            if (tabPage == null)
            {
                for (int i = 0; i < AlphaColorEditorCustomColorControl.CustomColorValues.Count; i++)
                {
                    if (AlphaColorEditorCustomColorControl.CustomColorValues[i].Color.A == this.currentAlpha && AlphaColorEditorCustomColorControl.CustomColorValues[i].Color.R == this.currentValue.R && AlphaColorEditorCustomColorControl.CustomColorValues[i].Color.G == this.currentValue.G && AlphaColorEditorCustomColorControl.CustomColorValues[i].Color.B == this.currentValue.B)
                    {
                        this.lb_Custom.SelectedItem = AlphaColorEditorCustomColorControl.CustomColorValues[i].Color;
                        tabPage = this.custom_page;
                        break;
                    }
                }
            }
            if (tabPage == null)
            {
                tabPage = this.custom_page;
            }
            this.tabControl.SelectedTab = tabPage;

            this.tabControl.SelectedIndexChanged += this.tabControl_SelectedIndexChanged;
            this.lb_Custom.ItemClick += this.lb_Custom_ItemClick;
            this.lb_Web.Click += this.lb_Web_Click;
            this.lb_System.Click += this.lb_System_Click;
            this.aTrackBar.Scroll += this.aTrackBar_Scroll;
            this.saveButton.Click += this.saveButton_Click;
        }

        public void End()
        {
            this.edSvc = (IWindowsFormsEditorService)null;
        }

        #endregion

        #region 私有方法

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.tabControl.SelectedTab.Tag.ToString() == this.custom_page_str)
            {
                this.lb_Custom.Select();
                this.lb_Custom.Focus();
            }
            else if (this.tabControl.SelectedTab.Tag.ToString() == this.web_page_str)
            {
                this.lb_Web.Select();
                this.lb_Web.Focus();
            }
            else if (this.tabControl.SelectedTab.Tag.ToString() == this.system_page_str)
            {
                this.lb_System.Select();
                this.lb_System.Focus();
            }
        }

        private void aTrackBar_Scroll(object sender, EventArgs e)
        {
            this.currentAlpha = this.aTrackBar.Value;
            this.aValueLabel.Text = "当前值:" + this.aTrackBar.Value.ToString();
            this.preview.AlphaColor = Color.FromArgb(this.currentAlpha, this.currentValue);
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            bool find = false;
            Color tmp = Color.FromArgb(this.currentAlpha, this.currentValue);

            if (tmp.A == byte.MinValue && tmp.R == byte.MinValue && tmp.G == byte.MinValue && tmp.B == byte.MinValue)
            {
                tmp = Color.Empty;
                find = true;
            }
            if (tmp.A == byte.MinValue && tmp.R == byte.MaxValue && tmp.G == byte.MaxValue && tmp.B == byte.MaxValue)
            {
                tmp = Color.Transparent;
                find = true;
            }
            if (!find && this.currentAlpha == byte.MaxValue && this.currentValue.IsSystemColor)
            {
                foreach (Color obj in this.lb_System.SystemColorValues)
                {
                    if (obj.Name == this.currentValue.Name)
                    {
                        tmp = (Color)obj;
                        find = true;
                        break;
                    }
                }
            }
            if (!find && this.currentAlpha == byte.MaxValue)
            {
                foreach (Color obj in this.lb_Web.WebColorValues)
                {
                    if (obj.A == this.currentValue.A && obj.R == this.currentValue.R && obj.G == this.currentValue.G && obj.B == this.currentValue.B)
                    {
                        tmp = (Color)obj;
                        find = true;
                        break;
                    }
                }
            }
            this.alphaColor = tmp;

            this.edSvc.CloseDropDown();
        }

        private void lb_System_Click(object sender, EventArgs e)
        {
            if (this.lb_System.SelectedItem == null)
                return;

            this.lb_Custom.SelectedItem = null;
            this.lb_Web.SelectedItem = null;
            if (this.IsEmptyOrTransparent((Color)this.lb_System.SelectedItem))
            {
                this.currentAlpha = byte.MinValue;
            }
            else if (this.currentAlpha == byte.MinValue && !this.IsEmptyOrTransparent((Color)this.lb_System.SelectedItem))
            {
                this.currentAlpha = byte.MaxValue;
            }
            this.currentValue = (Color)this.lb_System.SelectedItem;
            this.aTrackBar.Value = this.currentAlpha;
            this.aValueLabel.Text = "当前值:" + this.aTrackBar.Value.ToString();
            this.aGroupBox.Visible = !this.IsEmptyOrTransparent(this.currentValue);
            this.preview.AlphaColor = Color.FromArgb(this.currentAlpha, this.currentValue);
        }

        private void lb_Web_Click(object sender, EventArgs e)
        {
            if (this.lb_Web.SelectedItem == null)
                return;

            this.lb_Custom.SelectedItem = null;
            this.lb_System.SelectedItem = null;
            if (this.IsEmptyOrTransparent((Color)this.lb_Web.SelectedItem))
            {
                this.currentAlpha = byte.MinValue;
            }
            else if (this.currentAlpha == byte.MinValue && !this.IsEmptyOrTransparent((Color)this.lb_Web.SelectedItem))
            {
                this.currentAlpha = byte.MaxValue;
            }
            this.currentValue = (Color)this.lb_Web.SelectedItem;
            this.aTrackBar.Value = this.currentAlpha;
            this.aValueLabel.Text = "当前值:" + this.aTrackBar.Value.ToString();
            this.aGroupBox.Visible = !this.IsEmptyOrTransparent(this.currentValue);
            this.preview.AlphaColor = Color.FromArgb(this.currentAlpha, this.currentValue);
        }

        private void lb_Custom_ItemClick(object sender, EventArgs e)
        {
            if (this.lb_Custom.SelectedItem == null)
                return;

            this.lb_Web.SelectedItem = null;
            this.lb_System.SelectedItem = null;
            if (this.currentAlpha == byte.MinValue && !this.IsEmptyOrTransparent((Color)this.lb_Custom.SelectedItem))
            {
                this.currentAlpha = byte.MaxValue;
            }
            this.currentValue = this.ConvertUnAlphaColor((Color)this.lb_Custom.SelectedItem);
            this.aTrackBar.Value = this.currentAlpha;
            this.aValueLabel.Text = "当前值:" + this.aTrackBar.Value.ToString();
            this.aGroupBox.Visible = !this.IsEmptyOrTransparent(this.currentValue);
            this.preview.AlphaColor = Color.FromArgb(this.currentAlpha, this.currentValue);
        }

        /// <summary>
        /// 转换成对应非透明颜色
        /// </summary>
        /// <param name="color">要转换颜色</param>
        /// <returns></returns>
        private Color ConvertUnAlphaColor(Color color)
        {
            if (color.IsEmpty || (color.A == byte.MinValue && color.R == byte.MinValue && color.G == byte.MinValue && color.B == byte.MinValue))
                return Color.Empty;

            if (color == Color.Transparent || (color.A == byte.MinValue && color.R == byte.MaxValue && color.G == byte.MaxValue && color.B == byte.MaxValue))
                return Color.Transparent;

            if (color.IsSystemColor)
                return color;

            foreach (Color obj in this.lb_Web.WebColorValues)
            {
                if (obj.A == color.A && obj.R == color.R && obj.G == color.G && obj.B == color.B)
                {
                    return obj;
                }
            }

            return Color.FromArgb(byte.MaxValue, color);
        }

        /// <summary>
        /// 指定颜色是否为透明或空
        /// </summary>
        /// <param name="color">指定颜色</param>
        /// <returns></returns>
        private bool IsEmptyOrTransparent(Color color)
        {
            if (
                (color.A == byte.MinValue && color.R == byte.MinValue && color.G == byte.MinValue && color.B == byte.MinValue) ||
                (color.A == byte.MinValue && color.R == byte.MaxValue && color.G == byte.MaxValue && color.B == byte.MaxValue)
                )
                return true;

            return false;
        }

        #endregion
    }
}
